<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="bootstrap/dist/css/bootstrap.min.css">
</head>

<body>
<div id="app" class="container">
    <comment @add-comment="addComment"></comment>
    <ul>
        <li v-for="item in commentList" :key="item.id">
            <h3>评论人：{{ item.name }}</h3>
            <p >评论内容：{{ item.content}}</p>
        </li>
    </ul>
</div>
<script src="../vue.js"></script>
<script>
    let vm = new Vue({
        el: '#app',
        data: {
            commentList: [
                {id: 1, name: 'tom', content: 'jerrycomehere'},
                {id: 2, name: 'jerry', content: 'jerrycomehere'},
                {id: 3, name: 'jack', content: 'jerrycomehere'}
            ]
        },
        methods: {
            addComment(name, content) {
                let id;
                if (this.commentList.length === 0) {
                    id = 1
                }else{
                    id=this.commentList[this.commentList.length-1].id+1
                }
                this.commentList.push({
                    id: id,
                    name: name,
                    content: content
                })
            }
        },
        components: {
            comment: {
                template: `
                <div>
                    <div class="form-group">
                    <label>评论人：</label>
                     <input type="text" class="form-control" placeholder="请输入姓名" v-model="name">
                    </div>
                    <div class="form-group">
                     <label>评论内容：</label>
                     <textarea class="form-control" cols="30" rows="10" v-model="content"></textarea>
                     </div>
                    <button class="btn btn-default" @click="sendComment">发表评论</button>
                 </div>
                `,
                data() {
                    return {
                        name: '',
                        content: ''
                    }
                },
                methods: {
                    sendComment() {
                        this.$emit('add-comment', this.name, this.content)
                    }
                }
            }
        }
    })
</script>
</body>

</html>